(function ($) {
  var curOffset = 0, curQuery;
  $(document).on('ready', function () {
    service.init();
    onSearchChanging();
    bindEvents();
  });

  function bindEvents() {
    $('.query').on('input', function () {
      var elem = $(this).parent().find('.reset_btn');
      this.value.length > 0 ? elem.show() : elem.hide();
    })

    $('#result_page .query').on('focus', function () {
      if (this.value.length > 0) $('#result_page .reset_btn').show();
    })

    $('#result_page .query').on('blur', function () {
      $('#result_page .reset_btn').hide();
    })

    $('.reset_btn').on('touchstart mousedown', function () {
      $('.query').val('');
      $(this).hide();
    })

    $('.search_btn').on('click', function () {
      var q = $(this).siblings('.query').val();
      if (!q) return;

      if (location.hash && location.hash == '#' + q) {
        onSearchChanging();
      } else {
        pushSearchState(q);
      }
    });

    $('#result ul').on('click', 'li', function () {
      location.href = 'detail.html?id=' + encodeURIComponent($(this).data('id'));
    });

    $('.list_more').on('click', function () {
      search(curQuery);
    })

    $(window).on('hashchange', function () {
      onSearchChanging();
    })
  }

  function onSearchChanging() {
    curOffset = 0;
    if (!location.hash) {
      $('#search_page').show();
      $('#result_page').hide();
      $('#search_page .query').val('');
      return;
    }

    showSearchResult();
    var q = location.hash.substr(1);
    $('#result_page .query').val(decodeURIComponent(q));
    doSearch(q)
  }

  function showSearchResult() {
    $('#search_page').hide();
    $('#result_page').show();
  }

  function doSearch(q) {
    curQuery = q;
    resetResult();
    search(curQuery);
  }

  function pushSearchState(q) {
    var index = location.href.indexOf('#');
    var url = index > 0 ? location.href.substr(0, index) : location.href;
    location.href = url + '#' + q;
  }

  function resetResult() {
    $('#result').hide();
    $('#empty').hide();
    $('#result ul').html('');
  }

  function showEmpty() {
    if(!curOffset) {
      $('#result').hide();
      $('#empty').show();
    }
  }

  function createNovelElem(item) {
    var li = $('<li><span class="novel_name"></span><span class="author"></span></li>');
    li.attr('data-id', item.novel_id);
    li.find('.novel_name').text(item.novel_name);
    li.find('.author').text(item.author);
    return li;
  }

  function search(query) {
    service.search(query, function (data) {
      if (!data || !data.total_number) {
        showEmpty();
        return;
      }
      $('#result_info').text('（共' + data.total_number + '部作品）');
      var ulElem = $('#result ul');
      var liElems = [];
      for (var i = 0; i < data.items.length; ++i) {
        var item = data.items[i];
        liElems.push(createNovelElem(item));
      }
      ulElem.append(liElems);
      $('#result').show();
      curOffset += data.items.length;
      if (data.total_number <= curOffset + 1) {
        $('.list_more').hide();
      } else {
        $('.list_more').show();
      }
    }, function () {
      showEmpty();
    }, curOffset);
  }

})(Zepto);